﻿var current_html = null;
var page_length_obj = new Object();
var page_cancel_fun;

$("form").submit(function(){
    return false;
});
function _libTable(controls,data,index,size,maxIndex){
 this.controls = controls;
 this.data = data;
 this.index = index;
 this.size = size;
 this.maxIndex = maxIndex;
}


function libTable(head,data,index,size,maxIndex){
 this.head = head;
 this.data = data;
 this.index = index;
 if(page_length_obj[current_html])
  this.size = page_length_obj[current_html];
 else
  this.size = size;
 this.maxIndex = maxIndex;
}
String.prototype.Append = function(str){
 var temp = String(this);
 temp += str;
 return temp;
}


var tempData;
var tempControls;

var btn_map = [
 {type:"modify",title:sbutton.modify,src:"./images/edit.gif"},
 {type:"del",title:sbutton.del,src:"./images/del.png"},
 {type:"save",title:sbutton.saves,src:"./images/add.gif"},
 {type:"cancel",title:sbutton.cancel,src:"./images/back.gif"}
];


function create_frame(id,thead,tbody,tfoot){
  var table = document.getElementById(id);
  table.appendChild(thead);
  table.appendChild(tbody);
  table.appendChild(tfoot);
}



function create_control(fun,cell,row_id,param){
 var b = document.createElement("img");
 switch(fun.type){
   case "modify":
    b.setAttribute("src","./images/edit.gif");
    b.setAttribute("title",sbutton.modify);
    $(b).click(function(){reset_cell(cell,row_id,this,fun,param)})
   break;
   case "del":
    b.setAttribute("src","./images/del.png");
    b.setAttribute("title",sbutton.del);
    $(b).click(function(){delete_cell(row_id,this,fun,param)})
   break;
    case "add":
    b.setAttribute("src","./images/add.gif");
    b.setAttribute("title",sbutton.add);
    $(b).click(function(){delete_cell(row_id,this,fun,param)})
   break;
   case "other":
    b.setAttribute("src","./images/other.gif");
    b.setAttribute("title",sbutton.other);
    $(b).click(function(){reset_cell(cell,row_id,this,fun,param)})
   break;
   default:
 }
 b.className = "list_btn";
 cell.appendChild(b);
}


function reset_cell(current_cell,row_id,obj,fun,param){
 var current_row = obj.parentNode.parentNode;
 var row_childNodes = current_row.childNodes;
 var temp_cell = new Array();
 for(var c = 0;c < row_childNodes.length;c++){
  var cell = row_childNodes[c];


  if(c < tempControls.length){
  temp_cell.push(cell.innerHTML);
  if(tempControls[c].control != null){
     with(tempControls[c].control){
      switch(type){
       case "input":
        var input = document.createElement("input");
        for(var a in attr){
         input.setAttribute(a,attr[a]) ;
        };
        input.value = cell.innerHTML;
        cell.innerHTML = "";
        cell.appendChild(input);
       break;
       default :
        cell.innerHTML="--"
      }
     }
  }}
 }

 obj.parentNode.innerHTML = "";
 var b = document.createElement("img");
 b.setAttribute("src","./images/oks.gif");
 b.setAttribute("title",sbutton.saves);
 b.className = "list_btn";
 $(b).click(function(){save_cell(row_id,this,fun,param)})
 current_cell.appendChild(b);

 var c = document.createElement("img");
 c.setAttribute("src","./images/back.gif");
 c.setAttribute("title",sbutton.cancel);
 c.className = "list_btn";
 var fun_tmp = new Object();
 fun_tmp.fun = page_cancel_fun;
 $(c).click(function(){back_cell(row_id,this,fun_tmp,temp_cell,param)})
 current_cell.appendChild(c);
}

function save_cell(row_id,obj,fun,param){
 var current_row = obj.parentNode.parentNode;
 var row_childNodes = current_row.childNodes;

 var num = 0;
 for(var i in param){
  var p = param[i];

  if(row_childNodes[num].childNodes[0].value ){
   row_childNodes[num].innerHTML = row_childNodes[num].childNodes[0].value;
  }
  param[i] = row_childNodes[num].innerHTML;
  num ++;
 }


 fun.fun(param);

}

function delete_cell(row_id,obj,fun,param){
 fun.fun(param);
}
function back_cell(row_id,obj,fun,temp_cell,param){

  param.id = parseInt(param.id) +1;
 fun.fun(param);

}


function _print_table(tab_id,tab_obj,fun_arr,submit_fun){
 temp_tab_id = tab_id;
 var thead = document.createElement("thead");
 var tbody = document.createElement("tbody");
 var tfoot = document.createElement("tfoot");
  create_frame(tab_id,thead,tbody,tfoot);

  var head_row = document.createElement("tr");
  var control_row = document.createElement("tr");

  var controls = tab_obj.controls;
  tempControls = controls;

  for(var h in controls){
   var _head = controls[h].head;
   var head_cell = document.createElement("th");
   head_cell.innerHTML = _head;
   head_row.appendChild(head_cell);
  }
  var h_btn_cell = document.createElement("th");
  h_btn_cell.innerHTML = msg.op;
  head_row.appendChild(h_btn_cell);


  tempData = GetPage(tab_obj.data,256,1);

  for(row in tempData)
  {
   var row_id = parseInt(row) + 1;
   var data_row = document.createElement("tr");
   var temp_obj = new Object();
   var param = new Object();
   for(cell in tempData[row]){
    data_cell = document.createElement("td");
    var temp_data = tempData[row][cell];
    data_cell.innerHTML = temp_data;
    data_row.appendChild(data_cell);
    param[cell] = temp_data;
   }
   param.id = row;
   d_btn_cell = document.createElement("td");

   if(fun_arr)
   for(var i in fun_arr){
    var fun = fun_arr[i];
    create_control(fun,d_btn_cell,row_id,param);
   }
   data_row.appendChild(d_btn_cell);
   tbody.appendChild(data_row);
    }

  for(var c in controls){
   var _control = controls[c].control;
   var control_cell = document.createElement("td");
   if(_control != null){
    with(_control){
     switch(type){
      case "input":
       var input = document.createElement("input");
       for(var a in attr){
        input.setAttribute(a,attr[a]) ;
       };
       control_cell.appendChild(input);
      break;
      default :
       control_cell.innerHTML="--"
     }
    }
   }else{
    control_cell.innerHTML="--";
   }
   control_row.appendChild(control_cell);
  }
  var c_btn_cell = document.createElement("td");
  var add_new = document.createElement("img");
  add_new.setAttribute("src","./images/add.gif");
  add_new.setAttribute("title",sbutton.add);
  add_new.className = "list_btn";
  $(add_new).click(submit_fun)
  c_btn_cell.appendChild(add_new);
  control_row.appendChild(c_btn_cell);


  thead.appendChild(head_row);
  tbody.appendChild(control_row);
  $("#" + tab_id + " tr").hover(function(){
   $(this).css("background-color","#efefef");
  },function(){
   $(this).css("background-color","#F3F5F8");
  });

}






function print_table(id,tabObj,fun_arr, isAll_param){

 tabObj.maxIndex = Math.ceil(tabObj.data.length/tabObj.size);
 var table = $("#" + id);
 table.html(" ");
 var obj = tabObj;
 tempData = GetPage(obj.data,obj.size,obj.index);

 var headRow = "";
 if(obj.head){
  for(cell in obj.head){
   var head = obj.head[cell];
   headRow += '<th  style="cursor:default; ">'+head+'</th>';
  }
  table.append('<tr>' + headRow + '</tr>');
 }

 for(row in tempData)
 {
  var cellRow = "";
  var param='';
  var cell_id = 0;
  for(cell in tempData[row]){
   var text = tempData[row][cell];
   var out_text = "";
   if(typeof(text)=="undefined"){
       return false;
   }
   if(text.length != undefined && current_html == "logs")
   for(var i = 0;i<text.length;i++){
    out_text += text.substring(i,i+1);
    if(i >= 20 && i%20 == 0 )
    out_text +="<br/>";
   }
   else
   out_text = text+"";
   if (/[^\d]/.test(out_text)&&checkmac(out_text)){
    if(out_text.indexOf("[")>=0&&out_text.indexOf("]")>=0){
    	out_text = out_text.split("]")[1];
    	}
    if(out_text.indexOf("img_")>=0&&out_text.indexOf("|")>=0){
    	out_text = '<input type="image" src="./images/'+out_text.split("img_")[1].split("|")[0]+'.png" title="'+out_text.split("|")[1]+'" />'+out_text.split("|")[1];
    	}
    }
   cellData = '<td><div style="font-size:12px;">'+out_text+'</div></td>';
   cellRow += cellData;

   if(cell_id==0){
   	if(checkmac(out_text)){
   			if(/[^\d]/.test(tempData[row][cell])&&(tempData[row][cell]+"").indexOf("[")>=0&&(tempData[row][cell]+"").indexOf("]")>=0){
   				param+='\''+tempData[row][cell].split("[")[1].split("]")[0]+'\''+',\''+tempData[row][cell].split("]")[1]+'\'';
   			}else{
   				 param='\''+tempData[row][cell]+'\'';
   				}
   		}else{
   			param='\''+tempData[row][cell]+'\'';
   			}
   }else{
   		if(checkmac(out_text)){
    		if(isAll_param&&/[^\d]/.test(tempData[row][cell])&&(tempData[row][cell]+"").indexOf("[")>=0&&(tempData[row][cell]+"").indexOf("]")>=0){
     			param+=',\''+tempData[row][cell].split("[")[1].split("]")[0]+'\''+',\''+tempData[row][cell].split("]")[1]+'\'';
   			}else{
   				param+=',\''+tempData[row][cell]+'\'';
   			}
  		}else{
  			param+=',\''+tempData[row][cell]+'\'';
  			}
   }
   cell_id ++;
 }
  if(fun_arr){
   cellData='<td>';
   for(var i in fun_arr){
    var fun = fun_arr[i];
      if(fun.type == "modify"){
     var val_str = sbutton.modify;
     p = param;
     cellData += '<input type="image" src="./images/edit.gif" title="'+val_str+'"   onclick="'+fun.name+'('+p+')"/>&nbsp;&nbsp;&nbsp;';
    }
     if(fun.type == "other"){
     var val_str = sbutton.other;
     p = param;
     cellData += '<input type="image" src="./images/other.gif" title="'+val_str+'"   onclick="'+fun.name+'('+p+')"/>&nbsp;&nbsp;&nbsp;';
    }
    if(fun.type == "add"){
     var val_str = sbutton.add;
     p = param;
     cellData += '<input type="image" src="./images/add.gif" title="'+val_str+'"   onclick="'+fun.name+'('+p+')"/>&nbsp;&nbsp;&nbsp;';
    }
    if(fun.type == "del"){
     var val_str = sbutton.del;
      p = param;
     cellData += '<input type="image" src="./images/del.png" title="'+val_str+'"   onclick="'+fun.name+'('+p+')"/>';
    }
    
    }
   cellRow += cellData+'</td>';
  }
  table.append("<tr>"+cellRow+"</tr>");
 }



  $("#" + id + " tr").hover(function(){
   $(this).css("background-color","#efefef");
  },function(){
   $(this).css("background-color","#F3F5F8");
  });
  var cols;
  if(obj.head)
    cols = obj.head.length;
  else cols = $("#"+id).siblings("thead").find("th").length;



  var tdStr= '<tr><td  class="frame_tab_center" id="pageTd'+id+'" colspan="'+cols+'" valign="middle">';
  var amount_str = sbutton.amount_str;
  tdStr = tdStr.Append('<div style="float:left; height:23px;line-height:23px"><span style="float:left;font-size:12px">&nbsp;&nbsp;&nbsp;&nbsp;' + amount_str + ':&nbsp;&nbsp;</span><input type="text" id="setSize'+id+'" class="set_text" value="'+obj.size+'" size="2" maxlength="2"/> &nbsp;');
  var set_str = sbutton.apply;
  tdStr = tdStr.Append('&nbsp;&nbsp;<input type="image" src="./images/oks.gif" id="sizeBtn'+id+'"  title="'+set_str+'" class="set_btn"/> &nbsp;&nbsp;&nbsp;&nbsp;</div><div style="float:left">');
  tdStr = tdStr.Append('<a id="frist'+id+'" style="height:20px;width:25px;font-size:12px;cursor:pointer">'+sbutton.first+'</a>&nbsp;&nbsp;');
  tdStr = tdStr.Append('<a id="font'+id+'" style="height:20px;width:25px;font-size:12px;cursor:pointer">'+sbutton.previous+'</a>&nbsp;&nbsp;');
      tdStr = tdStr.Append('<a id="next'+id+'" style="height:20px;width:25px;font-size:12px;cursor:pointer">'+sbutton.next+'</a>&nbsp;&nbsp;');
      tdStr = tdStr.Append('<a id="last'+id+'" style="height:20px;width:25px;font-size:12px;cursor:pointer">'+sbutton.last+'</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
  tdStr = tdStr.Append('<select id="page_sel'+id+'" style="width:60px;text-align:center"></select>');

  var all_am_str = sbutton.total;

  tdStr = tdStr.Append('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-size:12px">'+all_am_str + ':'+tabObj.data.length+'&nbsp;'+sbutton.row+'&nbsp;</span></div>');
  tdStr = tdStr.Append("</td></tr>");

  table.append(tdStr);
  var sle_obj=document.getElementById("page_sel" + id);
  if(sle_obj==null){
      return false;
  }
  for(var i = 1;i<=tabObj.maxIndex;i++){
   var text = i + "/" + tabObj.maxIndex;
   var opt =new Option(text,i);
   if(jQuery.browser.msie){
    sle_obj.add(opt,-1);
   }else{
    sle_obj.add(opt,null);
   }
  }
  selectObj_chose_set(sle_obj,tabObj.index);
  $("#page_sel"+id).bind("change",function(){
    if(tempData.length > 0){
    tabObj.size = $("#setSize" + id).val();
    tabObj.index = this.value;
    print_table(id,tabObj,fun_arr, isAll_param);

    }

  });

  $("#sizeBtn"+ id).click(function(){
   if(tempData.length > 0){
   var size = $("#setSize" + id).val()
   if(!isNaN(size)){
   size = parseInt(size);
   if(size <= 1)size = 1;
   tabObj.size = ''+size;
   page_length_obj[current_html] = tabObj.size;
    tabObj.index = 1;
    print_table(id,tabObj,fun_arr, isAll_param);
    }
   }
  })

  $("#frist"+ id).click(function(){
   if(tempData.length > 0){
   tabObj.index = 1;
    print_table(id,tabObj,fun_arr, isAll_param);}
  })

  $("#font"+ id).click(function(){
   if(tempData.length > 0){
   if(tabObj.index != 1){
    tabObj.index -= 1;
    print_table(id,tabObj,fun_arr, isAll_param);

   } }
  })

  $("#next"+ id).click(function(){
   if(tempData.length > 0){
   if(tabObj.index != tabObj.maxIndex ){
    tabObj.index += 1;
    print_table(id,tabObj,fun_arr, isAll_param);
   } }
  })

  $("#last"+ id).click(function(){
   if(tempData.length > 0){
   tabObj.index = tabObj.maxIndex;
   print_table(id,tabObj,fun_arr, isAll_param); }
  })

}
var arrPage;
var GetPage = function(data,pageSize,pageIndex){
 var temp = data;
 var pageSize = pageSize;
 var pageIndex = pageIndex;
 var arrPage = new Array();;
 for(var i = pageSize*(pageIndex-1);i<pageSize*pageIndex;i++)
 {
  if(i<temp.length)
  arrPage.push(temp[i]);
 }
 return arrPage;
}
function create(type){
 return document.createElement(type);
}
function print_bw_tab(head,data,para,fun){
 var add = fun.add;
 var del = fun.del;
 var type = fun.type;
 var tab = create("table");
 tab.cellPadding = "1";
 tab.cellSpacing = "0";
 tab.className = "bw_tab";
 var tbody = create("tbody");
 var thead = create("thead");
 var h_r = create("tr");
 var h_c= create("td");
 h_c.innerHTML = head;
 h_c.className = "bw_tab_head";
 h_r.appendChild(h_c);
 h_c= create("td");
 h_c.style.backgroundColor="#bcdfe2";
 h_c.width = "15%";
 var btn = create("img");
 btn.src = "images/add.gif";
 btn.style.cursor = "pointer";
 btn.onclick = function(){add(type);};
 h_c.appendChild(btn);
 btn = create("img");
 btn.src = "images/del.png";
 btn.style.cursor = "pointer";
 btn.style.marginLeft = "5px";
 btn.onclick = function(){del(type,"",fun.divId)};
 h_c.appendChild(btn);
 h_r.appendChild(h_c);
 thead.appendChild(h_r);
 for(var i in data){
  var con = data[i][para];
  var c_r = create("tr");
  var c_c_l = create("td");
  c_c_l.innerHTML = con;
  var c_c_r = create("td");
  c_c_r.width = "15%";
  var btn = create("input");
  btn.type="checkbox";
  btn.title=sbutton.page_msg1;
  c_c_r.appendChild(btn);
  c_r.appendChild(c_c_l);
  c_r.appendChild(c_c_r);
  tbody.appendChild(c_r);
 }
 tab.appendChild(thead);
 tab.appendChild(tbody);
 return tab;
}
function data_for_page(data){
 var new_data = data;
 return new_data;
}
function Page_ctrl()
{
 this.pagelength = 10;
 this.currentpage = 1;
 this.arr = new Array();
 this.pages = 1;
 this.result = new Array();
 this.callback_func=null;
}
Page_ctrl.prototype.init = function(arr, cal_func)
{
 this.arr = arr;
 this.pages = Math.ceil(arr.length / this.pagelength);
 this.result = arr;
 if(cal_func){this.callback_func=cal_func;}
  else this.callback_func=function(data){};
}
Page_ctrl.prototype.get_foot = function(arr, cal_func)
{
 this.init(arr, cal_func);
 var _this = this;
 var obj = new Object();
  obj.div = document.createElement("div");
  obj.div.className ="page_layer";
  obj.data = arr.slice(0, this.pagelength);
 var tab = document.createElement("table");
 var row = tab.insertRow(0);
 var cell = row.insertCell(0);
 var text_btn_span = document.createElement("div");
  $(text_btn_span).css('float','left');
 var a_div = document.createElement("div");
  $(a_div).css('float','left');
 var sele = document.createElement("select");
  $(sele).change(function(){
         _this.select_page(this.value);
         _this.callback_func(_this.result);
         });
 var span_word = document.createElement("span");
  span_word.style.fontSize = "12px";
  span_word.innerHTML = sbutton.amount_str;
  $(span_word).css('float','left');
  text_btn_span.appendChild(span_word);
 var text_set = document.createElement("input");
  text_set.type = "text";
  text_set.value = _this.pagelength;
  text_set.style.width = "30px";
  text_set.maxLength = 2;
  $(text_set).css('float','left');
  text_btn_span.appendChild(text_set);
 var set_btn = document.createElement("input");
  set_btn.type = "image";
  set_btn.src="./images/oks.gif";
  set_btn.style.border = "1px solid #ccc";
  set_btn.style.height = "18px";
  set_btn.title = sbutton.apply;
  $(set_btn).click(function(){
       var pglen= parseInt(text_set.value);
       if(!getObj_msgbox(text_set,"int"))return;
       if(pglen <= 1)pglen = 1;
       _this.pagelength = pglen;
       _this.init(arr, cal_func);
       _this.result = arr.slice(0, _this.pagelength);
       _this.callback_func(_this.result);
       sele.innerHTML = "";
       for(var i=1; i<=_this.pages; i++ )
       {
        var opt = document.createElement("option");
         opt.value = i;
         opt.innerHTML = i+"/"+_this.pages;
        sele.appendChild(opt);
       }
      });
  $(set_btn).css('float','left');
  text_btn_span.appendChild(set_btn);
 var ln0 = document.createElement("a");
  ln0.innerHTML = "&nbsp;"+sbutton.first+"&nbsp;";
  ln0.href = "#javascript";
  $(ln0).click(function(){
         _this.first_page();
         _this.callback_func(_this.result);
         selectObj_chose_set(sele,_this.currentpage);
         });
 var ln1 = document.createElement("a");
  ln1.innerHTML = "&nbsp;"+sbutton.previous+"&nbsp;";
  ln1.href = "#javascript";
  $(ln1).click(function(){
         _this.prev_page();
          _this.callback_func(_this.result);
          selectObj_chose_set(sele,_this.currentpage);
         });
 var ln2 = document.createElement("a");
  ln2.href = "#javascript";
  ln2.innerHTML = "&nbsp;"+sbutton.next+"&nbsp;";
  $(ln2).click(function(){
         _this.next_page();
          _this.callback_func(_this.result);
          selectObj_chose_set(sele,_this.currentpage);
         });
 var ln3 = document.createElement("a");
  ln3.href = "#javascript";
  ln3.innerHTML = "&nbsp;"+sbutton.last+"&nbsp;";
  $(ln3).click(function(){
         _this.last_page();
          _this.callback_func(_this.result);
          selectObj_chose_set(sele,_this.currentpage);
         });
  cell.appendChild(text_btn_span);
   a_div.appendChild(ln0);
   a_div.appendChild(ln1);
   a_div.appendChild(ln2);
   a_div.appendChild(ln3);
   a_div.appendChild(sele);
  cell.appendChild(a_div);
  obj.div.appendChild(tab);
 for(var i=1; i<=this.pages; i++ )
 {
  var opt = document.createElement("option");
   opt.value = i;
   opt.innerHTML = i+"/"+this.pages;
  sele.appendChild(opt);
 }
 return obj;
}
Page_ctrl.prototype.select_page = function(index)
{
 this.currentpage = index;
 var start = (this.currentpage-1) * this.pagelength;
 var end = this.currentpage * this.pagelength ;
 this.result= this.arr.slice(start, end);
}
Page_ctrl.prototype.first_page = function()
{
 this.currentpage = 1;
 var start = (this.currentpage-1) * this.pagelength;
 var end = this.currentpage * this.pagelength ;
 this.result= this.arr.slice(start, end);
}
Page_ctrl.prototype.next_page = function()
{
 this.currentpage += 1;
 if(this.currentpage >= this.pages )this.currentpage = this.pages;
 var start = (this.currentpage-1) * this.pagelength;
 var end = this.currentpage * this.pagelength ;
 this.result= this.arr.slice(start, end);
}
Page_ctrl.prototype.prev_page = function()
{
 this.currentpage -= 1;
 if(this.currentpage <= 0 )this.currentpage = 1;
 var start = (this.currentpage-1) * this.pagelength;
 var end = this.currentpage * this.pagelength ;
 this.result= this.arr.slice(start, end);
}
Page_ctrl.prototype.last_page = function()
{
 this.currentpage = this.pages;
 var start = (this.currentpage-1) * this.pagelength;
 var end = this.currentpage * this.pagelength ;
 this.result = this.arr.slice(start, end);
}
